<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/4/9
 * Time: 下午4:26
 */

namespace Biz\module\Classes\mgt;

use Biz\model\ShopBasicInfo as mShopBasicInfo;
use Biz\model\trade\CustomizePayWay as mCustomizePayWay;
use Biz\model\trade\Order as mOrder;
use Biz\model\trade\OrderGoods as mOrderGoods;
use Biz\model\trade\OrderOperateLog as mOrderOperateLog;
use Biz\model\trade\OrderRefund as mOrderRefund;
use Biz\model\trade\ShopAddress as mShopAddress;
use Biz\pockets\constInterface\PayConfig;
use Biz\service\Event;
use Biz\service\TradeMsg as sTradeMsg;
use Biz\validate\trade\OrderRefund as vOrderRefund;
use Doraemon\amqp\Queue;
use Doraemon\service\User as sUser;
use Doraemon\tools\Log as tLog;


class OrderRefund extends \Biz\module\Classes\OrderRefund
{
    //TODO 待开发
    public function agreeRefund($shopId, $refundOrderNo, $addressId = 0, $isSys = false, $needOrderOpLog = true)
    {
        $returnGoods = false;
        $nowTime = time();

        //查询退款单信息
        $orderSku = self::getRefundOrderInfo($refundOrderNo);

        if ($orderSku['order_info']['pay_way'] == PayConfig::_PAY_WAYS_CUSTOMIZE_PAY_WAY) {
            $record = mCustomizePayWay::hasRefundRecord($refundOrderNo);
            if (!$record) {
                throw new \Exception('自定义支付未退款');
            }
        }


        //判断是否可以操作该退款单
        if (!$isSys && $orderSku['refund_info']['shop_id'] != $shopId) {
            throw new \Exception('该退款单不是您店铺的哦');
        }


        if ($orderSku['refund_info']['status'] != mOrderRefund::STATUS_NORMAL) {
            throw new \Exception('退款单状态不正确，不可操作');
        }

        $data = [];

        //判断并组装数据
        if ($orderSku['refund_info']['type_refund'] == mOrderRefund::TYPE_REFUND_ALL) {
            $returnGoods = true;

            $shopAddress = mShopAddress::getById($addressId);

            if (empty($shopAddress)) {
                throw new \Exception('请正确选择收货地址');
            }

            if ($shopAddress['shop_id'] != $shopId) {
                throw new \Exception('请正确填写退货收货地址');
            }

            $data['consignee_name'] = $shopAddress['accept_name'];
            $data['consignee_phone'] = $shopAddress['mobile'];
            $data['consignee_province'] = $shopAddress['province'];
            $data['consignee_city'] = $shopAddress['area'];
            $data['consignee_address'] = $shopAddress['address'];
            $data['consignee_zipcode'] = $shopAddress['zipcode'];
            $data['consignee_notice'] = $shopAddress['remark'];

            //商家同意退货后，7天内允许用户填写物流信息
            $data['allow_fill_logistics_close_time'] = date('Y-m-d H:i:s', $nowTime + mOrderRefund::ALLOW_FILL_LOGISTICS_CLOSE_TIME);

            //商家同意退货后，用户需在指定时间内填写物流信息,否则系统自动取消退货
            $data['auto_cancel_time'] = date('Y-m-d H:i:s', $nowTime + mOrderRefund::AUTO_CANCEL_TIME_FROM_AGREE_REFUND_GOODS);

            //同意退货的时间
            $data['agree_time'] = date('Y-m-d H:i:s', $nowTime);

            $data['status'] = mOrderRefund::STATUS_WAIT_REFUND_GOODS;

        } else {
            //同意退款的时间
            $data['agree_time'] = date('Y-m-d H:i:s', $nowTime);


            $data['status'] = mOrderRefund::STATUS_REFUNDING;

        }


        //操作退款记录表
        $result = mOrderRefund::update($data, ['refund_order_no' => $refundOrderNo, 'status' => mOrderRefund::STATUS_NORMAL]);

        //加日志
        tLog::refund_refund_info('商家同意退款，修改退款主表', [
            'shop_id' => $shopId,
            'refund_order_no' => $refundOrderNo,
            'params' => ['shop_id' => $shopId, 'refund_order_no' => $refundOrderNo, 'addressid' => $addressId, 'issys' => $isSys],
            'save_data' => $data
        ]);


        //增加同意退款的资金退款到退款队列
        if ($returnGoods) {
            //商家同意退货
            Event::push(Event::ORDER_REFUND_MGT_AGREE_SUCCESS, [
                'shop_id' => $orderSku['shop_id'],
                'order_no' => $orderSku['order_no'],
                'refund_order_no' => $refundOrderNo,
                'need_order_oplog' => $needOrderOpLog,
                'goods_id' => $orderSku['goods_id'],
                'sku_id' => $orderSku['sku_id'],
                'address_id' => $addressId,
                'method' => 1,
                'is_sys' => (int)$isSys,
                'uniq_idx' => ['refund_order_no', 'event_tag']
            ]);

        } else {

            //商家同意退款
            Event::push(Event::ORDER_REFUND_MGT_AGREE_SUCCESS, [
                'shop_id' => $orderSku['shop_id'],
                'order_no' => $orderSku['order_no'],
                'refund_order_no' => $refundOrderNo,
                'need_order_oplog' => $needOrderOpLog,
                'goods_id' => $orderSku['goods_id'],
                'sku_id' => $orderSku['sku_id'],
                'address_id' => $addressId,
                'method' => 2,
                'is_sys' => (int)$isSys,
                'uniq_idx' => ['refund_order_no', 'event_tag']
            ]);

            $data = [
                'is_sys' => (int)$isSys,
                'is_confirm_receipt' => ($orderSku['order_info']['status'] == mOrder::STATUS_SUCCESS) ? 1 : 0,
                'refund_integral' => $orderSku['refund_info']['refund_integral'],
                'refund_integral_amount' => $orderSku['refund_info']['refund_integral_amount'],
                'refund_packet_amount' => $orderSku['refund_info']['refund_packet_amount'],
                'refund_coupon_amount' => $orderSku['refund_info']['refund_coupon_amount'],
                'refund_activity_allowance' => $orderSku['refund_info']['refund_activity_allowance'],
                'refund_rmb' => $orderSku['refund_info']['refund_rmb'],
                'refund_shipping_amount' => $orderSku['refund_info']['refund_shipping_amount'],
                'refund_commission_amount' => $orderSku['refund_info']['refund_commission_amount'],
            ];

            $queueId = $this->intoQueue($orderSku['refund_info']['uid'], $shopId, $orderSku['order_no'], $refundOrderNo, $data);

            //加日志
            tLog::refund_refund_info('商家同意退款，开始插入数据到退款队列', [
                'shop_id' => $shopId,
                'refund_order_no' => $refundOrderNo,
                'params' => ['shop_id' => $shopId, 'refund_order_no' => $refundOrderNo, 'addressid' => $addressId, 'issys' => $isSys],
                'queue_id' => $queueId,
                'queue_data' => $data
            ]);


            self::syncRefundAmountRecord($orderSku['order_no'], $orderSku['sku_id'], [
                'refunded_amount' => $orderSku['refund_info']['refund_rmb'],
                'refund_status' => mOrderGoods::REFUND_STATUS_CAN_NOT_REFUND
            ], [
                'refunded_amount[+]' => $orderSku['refund_info']['refund_rmb'] + $orderSku['refund_info']['refund_shipping_amount'],
                'refunded_shipping_amount[+]' => $orderSku['refund_info']['refund_shipping_amount']
            ]);

            //结算系统对接
            $this->orderRefundIntoSettlementTransaction($shopId, $orderSku, $queueId, $nowTime, $isSys);


        }

        return true;


    }


    public function rejectRefund($shopId, $refundOrderNo, $data)
    {
        $nowTime = time();

        //判断表单是否正确
        if (!vOrderRefund::validate($data, 'reject')) {
            $msg = vOrderRefund::firstError();
            throw new \Exception($msg);
        }

        //查询退款单信息
        $orderSku = self::getRefundOrderInfo($refundOrderNo);

        //判断是否可以拒绝退款
        if ($orderSku['refund_info']['shop_id'] != $shopId) {
            throw new \Exception('抱歉没有操作该退款单的权限');
        }

        $canRejectStatus = [mOrderRefund::STATUS_NORMAL, mOrderRefund::STATUS_WAIT_CONFIRM_RECEIPT];

        if (!in_array($orderSku['refund_info']['status'], $canRejectStatus)) {
            throw new \Exception('不可操作该退款单');
        }

        //操作退款记录表
        $upData = array_merge([
            'reject_remark' => $data['reject_remark'],
            'reject_images_content' => $data['images_content'],
            'reject_time' => date('Y-m-d H:i:s'),
            'allow_change_time' => date('Y-m-d H:i:s', $nowTime + mOrderRefund::AUTO_AFTER_REJECT_CHANGE_FORM_TIME),
            'auto_cancel_time' => date('Y-m-d H:i:s', $nowTime + mOrderRefund::AUTO_AFTER_REJECT_CANCEL_TIME),
            'allow_launch_rights_stop_time' => date('Y-m-d H:i:s', $nowTime + mOrderRefund::AUTO_AFTER_REJECT_RIGHTS_STOP_TIME),
            'status' => mOrderRefund::STATUS_REJECT,
        ], mOrderRefund::getClearRightsInfoDataStruct());

        $result = mOrderRefund::update($upData, ['refund_order_no' => $refundOrderNo]);

        //商家拒绝退款的事件
        Event::push(Event::ORDER_REFUND_MGT_REJECT_SUCCESS, [
            'shop_id' => $orderSku['shop_id'],
            'order_no' => $orderSku['order_no'],
            'refund_order_no' => $refundOrderNo,
            'goods_id' => $orderSku['goods_id'],
            'sku_id' => $orderSku['sku_id'],
            'type_refund' => $orderSku['refund_info']['type_refund'],
            'remark' => $data['reject_remark'],
            'reject_time' => $upData['reject_time'],
            'uniq_idx' => ['refund_order_no', 'event_tag', 'reject_time']
        ]);

        //加日志
        tLog::refund_refund_info('商家拒绝退款，退款单主表的更新内容', [
            'shop_id' => $shopId,
            'refund_order_no' => $refundOrderNo,
            'params' => ['shop_id' => $shopId, 'refund_order_no' => $refundOrderNo, 'data' => $data],
            'up_data' => $upData
        ]);

        if ($result->rowCount() == 0) {
            throw new \Exception('操作异常');
        }

        //增加操作日志
        $result = mOrderOperateLog::createLog($orderSku['shop_id'], $orderSku['order_no'], mOrderOperateLog::TRADE_TYPE_ORDER_REFUND, [
            'operate_type' => mOrderOperateLog::OPERATE_TYPE_REJECT_REFUND_AMOUNT,
            'operate_user_type' => mOrderOperateLog::OPERATE_USERTYPE_MGT,
            'operate_uid' => $shopId,
        ], $refundOrderNo, vsprintf('拒绝退款 理由:%s', [
            $data['reject_remark']
        ]), $data['images_content'], $upData);

        if ($result->rowCount() == 0) {
            throw new \Exception('退款申请错误');
        }

        //给买家发送下行消息
        $msgCode = 202;
        if ($orderSku['refund_info']['type_refund'] == mOrderRefund::TYPE_REFUND_ALL) {
            $msgCode = 302;
        }

        if ($orderSku['order_info']['status'] == mOrder::STATUS_WAIT_SEND) {
            $shopName = mShopBasicInfo::getShopInfo($shopId, 'name');

            Queue::insertQueue("queue/Wechatnotice/toTag", [
                "totag" => \Biz\pockets\constInterface\BizWechatTagConfig::TAG_BIZ_RD_REFUND,
                "content" => vsprintf("［拒绝退款提醒］发货前订单：%s 商家 %s (%s)拒绝退款 退款单号：%s。拒绝理由：%s", [
                    $orderSku['refund_info']['order_no'],
                    $shopName,
                    $shopId,
                    $refundOrderNo,
                    $data['reject_remark']
                ])
            ], Queue::QUEUE_BIZ);
        }

        if (!in_array($orderSku['order_info']['from_source'], [mOrder::FROM_SOURCE_PARAGRAPH_MINIPROGRAM])) {

            sTradeMsg::addMsg($msgCode, $orderSku['refund_info']['uid'], [
                'refund_order_no' => $refundOrderNo,
                'reject_remark' => $data['reject_remark'],
                'img' => $orderSku['cover_img_thumb_url'],
            ]);
        }
    }


    //商家收到退货后，确认退款
    public function confirmRefund($shopId, $refundOrderNo, $isSys = false)
    {
        $nowTime = time();

        //查询退款单信息
        $orderSku = self::getRefundOrderInfo($refundOrderNo);

        //判断是否可以操作该退款单
        if (!$isSys) {
            if ($orderSku['refund_info']['shop_id'] != $shopId) {
                throw new \Exception('抱歉，无操作权限');
            }
        }

        if ($orderSku['refund_info']['status'] != mOrderRefund::STATUS_WAIT_CONFIRM_RECEIPT) {
            throw new \Exception('退款单状态不可操作');
        }

        //操作退款记录表
        $result = mOrderRefund::update([
            'confirm_receipt_time' => date('Y-m-d H:i:s', $nowTime),
            'status' => mOrderRefund::STATUS_REFUNDING
        ], ['refund_order_no' => $refundOrderNo, 'status' => mOrderRefund::STATUS_WAIT_CONFIRM_RECEIPT]);

        //加日志
        tLog::refund_refund_info('商家确认退款，退款表修改的内容', [
            'shop_id' => $shopId,
            'refund_order_no' => $refundOrderNo,
            'params' => ['shop_id' => $shopId, 'refund_order_no' => $refundOrderNo, 'issys' => $isSys],
            'up_data' => [
                'confirm_receipt_time' => date('Y-m-d H:i:s', $nowTime),
                'status' => mOrderRefund::STATUS_REFUNDING
            ]
        ]);


        if ($result->rowCount() == 0) {
            throw new \Exception('操作异常');
        }


        //增加操作日志
        $result = mOrderOperateLog::createLog($orderSku['shop_id'], $orderSku['order_no'], mOrderOperateLog::TRADE_TYPE_ORDER_REFUND, [
            'operate_type' => mOrderOperateLog::OPERATE_TYPE_CONFIRM_REFUND_AMOUNT,
            'operate_user_type' => $isSys ? mOrderOperateLog::OPERATE_USERTYPE_SYS : mOrderOperateLog::OPERATE_USERTYPE_MGT,
            'operate_uid' => $isSys ? 0 : $shopId,
        ], $refundOrderNo, $isSys ? '超时自动确认收货并退款' : '确认收货并退款', '');

        if ($result->rowCount() == 0) {
            throw new \Exception('退款申请错误');
        }


        //增加同意退款的资金退款到退款队列
        $data = [
            'is_sys' => (int)$isSys,
            'is_confirm_receipt' => ($orderSku['order_info']['status'] == mOrder::STATUS_SUCCESS) ? 1 : 0,
            'refund_integral' => $orderSku['refund_info']['refund_integral'],
            'refund_integral_amount' => $orderSku['refund_info']['refund_integral_amount'],
            'refund_packet_amount' => $orderSku['refund_info']['refund_packet_amount'],
            'refund_coupon_amount' => $orderSku['refund_info']['refund_coupon_amount'],
            'refund_activity_allowance' => $orderSku['refund_info']['refund_activity_allowance'],
            'refund_rmb' => $orderSku['refund_info']['refund_rmb'],
            'refund_shipping_amount' => $orderSku['refund_info']['refund_shipping_amount'],
            'refund_commission_amount' => $orderSku['refund_info']['refund_commission_amount'],
        ];

        $queueId = $this->intoQueue($orderSku['refund_info']['uid'], $shopId, $orderSku['order_no'], $refundOrderNo, $data);


        //商家收到退货的商品,确认退货
        Event::push(Event::ORDER_REFUND_MGT_CONFIRM_RECEIPT_SUCCESS, [
            'shop_id' => $orderSku['shop_id'],
            'order_no' => $orderSku['order_no'],
            'refund_order_no' => $refundOrderNo,
            'goods_id' => $orderSku['goods_id'],
            'sku_id' => $orderSku['sku_id'],
            'is_sys' => (int)$isSys,
            'uniq_idx' => ['refund_order_no', 'event_tag']
        ]);

        //加日志
        tLog::refund_refund_info('确认退款，退款确认金额， 入队列', [
            'shop_id' => $shopId,
            'refund_order_no' => $refundOrderNo,
            'params' => ['shop_id' => $shopId, 'refund_order_no' => $refundOrderNo, 'issys' => $isSys],
            'queue_id' => $queueId,
            'queue_data' => $data
        ]);

        self::syncRefundAmountRecord($orderSku['order_no'], $orderSku['sku_id'], [
            'refunded_amount' => $orderSku['refund_info']['refund_rmb'],
            'refund_status' => mOrderGoods::REFUND_STATUS_CAN_NOT_REFUND
        ], [
            'refunded_amount[+]' => $orderSku['refund_info']['refund_rmb'] + $orderSku['refund_info']['refund_shipping_amount'],
            'refunded_shipping_amount[+]' => $orderSku['refund_info']['refund_shipping_amount']
        ]);

        //结算系统对接
        $this->orderRefundIntoSettlementTransaction($shopId, $orderSku, $queueId, $nowTime, $isSys);

        if (!in_array($orderSku['order_info']['from_source'], [mOrder::FROM_SOURCE_PARAGRAPH_MINIPROGRAM])) {
            //给买家下发系统通知
            sTradeMsg::addMsg(303, $orderSku['refund_info']['uid'], [
                'refund_order_no' => $refundOrderNo,
                'refund_amount' => $orderSku['refund_info']['refund_amount'],
                'img' => $orderSku['cover_img_thumb_url'],
            ]);
        }

    }

    public function fillRightsProof($shopId, $refundOrderNo, $data)
    {
        $nowTime = date('Y-m-d H:i:s');
        //判断表单是否正确
        if (!vOrderRefund::validate($data, 'fillRightsProof')) {
            $msg = vOrderRefund::firstError();
            throw new \Exception($msg);
        }

        //查询退款单信息
        $orderSku = self::getRefundOrderInfo($refundOrderNo);

        //判断是否可以提交凭证
        if ($orderSku['refund_info']['shop_id'] != $shopId) {
            throw new \Exception('不可提交凭证');
        }

        if ($orderSku['refund_info']['status'] != mOrderRefund::STATUS_REJECT) {
            throw new \Exception('不可提交凭证');
        }

        if ($orderSku['refund_info']['rights_status'] != mOrderRefund::RIGHTS_STATUS_PROOFING) {
            throw new \Exception('等待平台介入才可以举证');
        }

        if (strtotime($orderSku['refund_info']['submit_proof_stop_time']) < time()) {
            throw new \Exception('已超过举证时间点');
        }

        //商家填写维权信息事件
        Event::push(Event::ORDER_REFUND_RIGHTS_MGT_FILL_SUCCESS, [
            'shop_id' => $shopId,
            'order_no' => $orderSku['order_no'],
            'refund_order_no' => $refundOrderNo,
            'goods_id' => $orderSku['goods_id'],
            'sku_id' => $orderSku['sku_id'],
            'proof_remark' => $data['proof_remark'],
            'images_content' => $data['images_content'],
            'now_time' => $nowTime,
            'uniq_idx' => ['refund_order_no', 'event_tag', 'now_time']
        ]);


        //加日志
        tLog::refund_rights_info('商家填写维权凭证', [
            'shop_id' => $shopId,
            'refund_order_no' => $refundOrderNo,
            'params' => ['shop_id' => $shopId, 'refund_order_no' => $refundOrderNo, 'data' => $data],
        ]);

        $textContent = !empty($data['proof_remark']) ? '：' . $data['proof_remark'] : '';

        //增加操作日志
        $result = mOrderOperateLog::createLog($orderSku['shop_id'], $orderSku['order_no'], mOrderOperateLog::TRADE_TYPE_ORDER_RIGHTS, [
            'operate_type' => mOrderOperateLog::OPERATE_TYPE_SUBMIT_PROOF,
            'operate_user_type' => mOrderOperateLog::OPERATE_USERTYPE_MGT,
            'operate_uid' => $shopId,
        ], $refundOrderNo, vsprintf('提交凭证%s', [
            $textContent
        ]), $data['images_content']);

        if ($result->rowCount() == 0) {
            throw new \Exception('提交凭证出错');
        }
    }

    public function prolongReceipt($shopId, $refundOrderNo)
    {
        $nowTime = time();

        //查询退款单信息
        $orderSku = self::getRefundOrderInfo($refundOrderNo);

        //判断是否可以延长收货
        if ($orderSku['refund_info']['shop_id'] != $shopId) {
            throw new \Exception('不可操作该退款单');
        }

        if ($orderSku['refund_info']['status'] != mOrderRefund::STATUS_WAIT_CONFIRM_RECEIPT) {
            throw new \Exception('不可操作该退款单');
        }

        if (strtotime($orderSku['refund_info']['prolong_receipt_time']) > 0) {
            throw new \Exception('已经延长一次不可再次延长');
        }

        //操作退款记录表
        $upData = [
            'prolong_receipt_time' => date('Y-m-d H:i:s', $nowTime + mOrderRefund::ALLOW_PROLONG_RECEIPT_TIME),
            'auto_confirm_receipt_time' => date('Y-m-d H:i:s', $nowTime + mOrderRefund::AUTO_CONFIRM_RECEIPT_TIME),
        ];
        $result = mOrderRefund::update($upData, ['refund_order_no' => $refundOrderNo]);


        //加日志
        tLog::refund_rights_info('商家延长退货的收货时间', ['shop_id' => $shopId, 'refund_order_no' => $refundOrderNo, 'up_data' => $upData]);

        if ($result->rowCount() == 0) {
            throw new \Exception('操作异常');
        }


        //商家延长退货的收货时间的事件
        Event::push(Event::ORDER_REFUND_MGT_PROLONG_RECEIPT_TIME_SUCCESS, [
            'shop_id' => $shopId,
            'order_no' => $orderSku['order_no'],
            'refund_order_no' => $refundOrderNo,
            'goods_id' => $orderSku['goods_id'],
            'sku_id' => $orderSku['sku_id'],
            'prolong_receipt_time' => $upData['prolong_receipt_time'],
            'uniq_idx' => ['refund_order_no', 'event_tag', 'prolong_receipt_time']
        ]);

        //增加操作日志
        $result = mOrderOperateLog::createLog($orderSku['shop_id'], $orderSku['order_no'], mOrderOperateLog::TRADE_TYPE_ORDER_REFUND, [
            'operate_type' => mOrderOperateLog::OPERATE_TYPE_PROLONG_RECEIPT_TIME,
            'operate_user_type' => mOrderOperateLog::OPERATE_USERTYPE_MGT,
            'operate_uid' => $shopId,
        ], $refundOrderNo, '收货时间延长3天', '', $upData);

        if ($result->rowCount() == 0) {
            throw new \Exception('延长收货时间失败');
        }

    }

    public function getDetail($refundOrderNo, $shopId, $reason = -1)
    {
        $data = [];

        //默认退货地址
        $defaultAddress = mShopAddress::getDefaultAddress($shopId);

        $refundInfo = mOrderRefund::getRefundOrderInfo($refundOrderNo, 'mgt');
        if ($refundInfo['shop_id'] != $shopId) {
            throw new \Exception('您无权查看此退款单号');
        }
        $arrayTime = [
            'apply_time' => $refundInfo['addtime'],
            'now_time' => time(),
            'auto_agree_time' => strtotime($refundInfo['auto_agree_time']),
            'submit_proof_stop_time' => strtotime($refundInfo['submit_proof_stop_time']),
            'auto_logistics_close_time' => strtotime($refundInfo['allow_fill_logistics_close_time']),
            'auto_confirm_receipt_time' => strtotime($refundInfo['auto_confirm_receipt_time'])
        ];
        $data['refund_order_no'] = $refundOrderNo;
        $data['order_no'] = $refundInfo['order_no'];
        $data['sku_id'] = $refundInfo['sku_id'];
        $data['refund_amount'] = $refundInfo['refund_amount'];
        $data['refund_integral_amount'] = $refundInfo['refund_integral_amount'];
        $data['refund_packet_amount'] = $refundInfo['refund_packet_amount'];
        $data['refund_coupon_amount'] = $refundInfo['refund_coupon_amount'];
        $data['refund_activity_allowance'] = $refundInfo['refund_activity_allowance'];
        $data['type_refund'] = $refundInfo['type_refund'];
        $data['type_refund_desc'] = $refundInfo['type_refund_desc'];
        $data['reason'] = $refundInfo['reason'];
        $data['reason_desc'] = $refundInfo['reason_desc'];
        $data['remark'] = $refundInfo['reject_remark'];
        $data['status'] = $refundInfo['status'];
        $data['status_desc'] = $refundInfo['status_desc'];
        $data['text_content'] = $refundInfo['refund_remark'];
        $data['images_content'] = $refundInfo['refund_images_content_url'];
        $data['receipt_status'] = $refundInfo['receipt_status'];
        $data['receipt_status_desc'] = $refundInfo['receipt_status_desc'];
        $data['time_info'] = $this->formatTime($arrayTime);
        $data['address_info'] = empty($refundInfo['consignee_address']) ? [
            'accept_name' => $defaultAddress['accept_name'],
            'mobile' => $defaultAddress['mobile'],
            'address' => $defaultAddress['address']
        ] : [
            'accept_name' => $refundInfo['consignee_name'],
            'mobile' => $refundInfo['consignee_phone'],
            'address' => $refundInfo['consignee_address']
        ];
        return $data;
    }

    public function getLogList($refundOrderNo, $shopId)
    {
        $list = [];
        $logList = mOrderOperateLog::getLogList([
            'refund_order_no' => $refundOrderNo,
            'trade_type' => [mOrderOperateLog::TRADE_TYPE_ORDER_REFUND, mOrderOperateLog::TRADE_TYPE_ORDER_RIGHTS],
            'shop_id' => $shopId,
            'ORDER' => ['addtime' => 'DESC']
        ]);

        $uids = array_filter(array_column($logList, 'operate_uid'), function ($v) {
            if ($v != 0) {
                return true;
            }
            return false;
        });

        $userInfos = sUser::getUsers($uids);

        $sysUserInfo = sUser::getSysUserInfo();

        array_walk($logList, function ($v, $k) use (&$list, $userInfos, $sysUserInfo) {
            $list[$k]['add_time'] = $v['addtime'];
            $list[$k]['text_content'] = $v['text_content'];
            $list[$k]['images_content'] = $v['images_content_url'];
            $list[$k]['user_info'] = [
                'user_type' => $v['operate_user_type'],
                'user_type_desc' => mOrderOperateLog::$operateUserTypeDesc[$v['operate_user_type']],
                'uid' => $v['operate_uid'],
                'name' => $v['operate_user_type'] == mOrderOperateLog::OPERATE_USERTYPE_SYS ? $sysUserInfo['nick'] : $userInfos[$v['operate_uid']]['nick'],
                'avatar_small' => $v['operate_user_type'] == mOrderOperateLog::OPERATE_USERTYPE_SYS ? $sysUserInfo['avatar_small'] : $userInfos[$v['operate_uid']]['avatar_small']
            ];
        });
        return $list;
    }

    public function getRightsDetail($refundOrderNo, $shopId)
    {
        $data = [];
        $refundInfo = mOrderRefund::getRefundOrderInfo($refundOrderNo, 'mgt');
        if ($refundInfo['shop_id'] != $shopId) {
            throw new \Exception('您无权查看此退款单号');
        }
        if ($refundInfo['rights_status'] == 0) {
            throw new \Exception('此单号没有维权信息');
        }
        $data['now_time'] = time();
        $data['order_no'] = $refundInfo['order_no'];
        $data['sku_id'] = $refundInfo['sku_id'];
        $data['launch_rights_time'] = $refundInfo['launch_rights_time'];
        $data['submit_proof_stop_time'] = strtotime($refundInfo['submit_proof_stop_time']);
        $data['rights_into_time'] = $refundInfo['rights_into_time'];
        $data['rights_remark'] = $refundInfo['rights_remark'];
        $data['rights_images_content'] = $refundInfo['rights_images_content_url'];
        $data['rights_process_method'] = $refundInfo['rights_process_method'];
        $data['rights_process_method_desc'] = $refundInfo['rights_process_method_desc'];
        $data['rights_judge_result'] = $refundInfo['rights_judge_result'];
        $data['rights_judge_result_desc'] = $refundInfo['rights_judge_result_desc'];
        $data['rights_judge_refund_amount'] = $refundInfo['rights_judge_refund_amount'];
        $data['rights_judge_refund_integral'] = $refundInfo['rights_judge_refund_integral'];
        $data['rights_judge_refund_rmb'] = $refundInfo['rights_judge_refund_rmb'];
        $data['rights_judge_remark'] = $refundInfo['rights_judge_remark'];
        $data['rights_judge_images_content'] = $refundInfo['rights_judge_images_content_url'];
        $data['rights_judge_time'] = $refundInfo['rights_judge_time'];
        $data['v_rights_status'] = $refundInfo['v_rights_status'];
        $data['v_rights_status_desc'] = $refundInfo['v_rights_status_desc'];

        $data['refund_info'] = [
            'refund_order_no' => $refundOrderNo,
            'type_refund' => $refundInfo['type_refund'],
            'type_refund_desc' => $refundInfo['type_refund_desc'],
            'reason' => $refundInfo['reason'],
            'reason_desc' => $refundInfo['reason_desc'],
            'refund_amount' => $refundInfo['refund_amount'],
            'refund_integral_amount' => $refundInfo['refund_integral_amount'],
            'refund_packet_amount' => $refundInfo['refund_packet_amount'],
            'refund_coupon_amount' => $refundInfo['refund_coupon_amount'],
            'refund_activity_allowance' => $refundInfo['refund_activity_allowance'],
            'refund_rmb' => $refundInfo['refund_rmb'],
            'reject_remark' => $refundInfo['reject_remark'],
        ];
        return $data;
    }
}